package com.fili;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;

/**
 * Created by Claudiu-Florin FILIMON
 * Date: Mar 2, 2007
 * Time: 12:36:21 PM
 * Revision: 0
 */
public class ZumaPanel extends JComponent implements MouseListener, MouseMotionListener
{
    private BallPath ballPath;
    private ArrayList<Ball> balls;
    private Broasca broasca;

    private boolean fired = false;
    private float fx, fy;
    private float sx, sy;


    public ZumaPanel()
    {
        ballPath = new BallPath();

        balls = new ArrayList<Ball>();
        for (int i = 0; i < 1000; i += 100)
            balls.add(new Ball(ballPath, i, Color.red));

        broasca = new Broasca(300, 200);

        addMouseListener(this);
        addMouseMotionListener(this);
    }


    protected void paintComponent(Graphics g)
    {
        g.setColor(Color.white);
        g.fillRect(0, 0, getWidth(), getHeight());

        ballPath.paint(g);
        for (Ball ball : balls)
            ball.paint(g);

        broasca.paint(g);

        if (fired)
        {
            g.setColor(Color.blue);
            g.fillOval((int) fx - DEF.BALL_RADIUS / 2, (int) fy - DEF.BALL_RADIUS / 2, DEF.BALL_RADIUS + 1, DEF.BALL_RADIUS + 1);
            g.setColor(Color.black);
            g.drawOval((int) fx - DEF.BALL_RADIUS / 2, (int) fy - DEF.BALL_RADIUS / 2, DEF.BALL_RADIUS, DEF.BALL_RADIUS);
        }
    }

    private void pushBall(int ballNum, float dist)
    {
        Ball b = balls.get(ballNum);

        b.pos += dist;
        if (b.pos > ballPath.len)
            b.pos = ballPath.len - 1;
        if (dist > 0)
        {
            if (ballNum < balls.size() - 1)
            {
                Ball b2 = balls.get(ballNum + 1);
                if (b2.pos - b.pos < DEF.BALL_RADIUS)
                    pushBall(ballNum + 1, b.pos - b2.pos + DEF.BALL_RADIUS);
            }
        }
        else
        {
            if (ballNum > 0)
            {
                Ball b2 = balls.get(ballNum - 1);
                if (b.pos - b2.pos < DEF.BALL_RADIUS)
                    pushBall(ballNum - 1, -(b2.pos - b.pos + DEF.BALL_RADIUS));
            }
        }
    }

    public void pushBalls(float dist)
    {
        pushBall(0, dist);
    }

    public void mouseClicked(MouseEvent e)
    {
    }

    public void mousePressed(MouseEvent e)
    {
        if (fired)
            return;
        fx = broasca.getX();
        fy = broasca.getY();
        Point p = broasca.getPlus(200);

        sx = (p.x - broasca.getX()) / 10.0f;
        sy = (p.y - broasca.getY()) / 10.0f;
        fired = true;
    }

    public void mouseReleased(MouseEvent e)
    {
    }

    public void mouseEntered(MouseEvent e)
    {
    }

    public void mouseExited(MouseEvent e)
    {
    }

    public void mouseDragged(MouseEvent e)
    {
    }

    public void mouseMoved(MouseEvent e)
    {
        broasca.setOrientation(e.getX(), e.getY());
    }

    public void updateStuff()
    {
        pushBalls(2);
        if (fired)
        {
            fx += sx;
            fy += sy;
        }

        if ((fx < 0) || (fy < 0) || (fx > getWidth()) || (fy > getWidth()))
            fired = false;

        if (fired)
        {
            for (int i = 0; i < balls.size(); i++)
            {
                Ball b = balls.get(i);
                Point p = ballPath.getPathPoint(b.pos);

                float dx = p.x - fx;
                float dy = p.y - fy;

                if (Math.sqrt(dx * dx + dy * dy) < DEF.BALL_RADIUS)
                {
                    balls.add(i + 1, new Ball(ballPath, b.pos + DEF.BALL_RADIUS / 2, Color.blue));
                    pushBall(i, 0.01f);
                    fired = false;
                    break;
                }
            }
        }

        if (balls.get(balls.size() - 1).pos >= ballPath.len - 5)
        {
            JOptionPane.showMessageDialog(this, "You loose :P");
            System.exit(1);
        }

    }
}
